<?php
class LoginApi extends BZ_Controller
{
    public function _init()
    {
        $this->load->helpers('common');
        $this->load->model('user_model', 'user');
        $this->load->model('user_account_model', 'user_account');
    }

    /**
     * 查询单个用户信息
     */
    public function searchOnlyUser()
    {
        $uid      = (int)$this->input->get_post('uid');
        $username = $this->input->get_post('user_name');
        $mobile   = $this->input->get_post('mobile');
        if (empty($uid) && empty($username) && empty($mobile)) {
            $this->ajaxJson(false, '参数有误');
        }
        if ($uid && !is_int($uid)) {
            $this->ajaxJson(false, 'uid编号不合法');
        }
        if ($mobile && !valid_mobile($mobile)) {
            $this->ajaxJson(false, '手机号码有误，请检查~');
        }
        //参数查询
        if ($uid) {
            $where = ['uid' => $uid];
        } elseif ($mobile) {
            $where = ['mobile_phone' => $mobile];
        } else {
            $where = ['user_name' => $username];
        }
        $data = $this->loginInfo($where);
        if (!$data['status']) {
            $this->ajaxJson($data);
        }
        $this->ajaxJson(true, '', $data['msg']);
    }

    /**
     * 添加用户,返回session数据
     */
    public function addUserGetUid()
    {
        $username       = $this->input->get_post('user_name') ? $this->input->get_post('user_name') : '';
        $mobile         = $this->input->get_post('mobile');
        $password       = $this->input->get_post('password');
        $alias_name     = $this->input->get_post('alias_name') ? $this->input->get_post('alias_name') : '';
        $user_email     = $this->input->get_post('user_email') ? $this->input->get_post('user_email') : '';
        $user_type      = $this->input->get_post('user_type') ? $this->input->get_post('user_type') : UTID_CUSTOMER;
        $parent_id      = $this->input->get_post('parent_id') ? $this->input->get_post('parent_id') : UTID_BEIZHU;
        $personal_photo = $this->input->get_post('personal_photo') ? $this->input->get_post('personal_photo') : '';
        $contacter      = $this->input->get_post('contacter') ? $this->input->get_post('contacter') : ''; //真实姓名
        $sex_id         = $this->input->get_post('sex_id') ? $this->input->get_post('sex_id') : 0; //0-保密，1-男 2-女
        $birthday       = $this->input->get_post('birthday') ? $this->input->get_post('birthday') : '0000-00-00'; //生日

        if (empty($mobile) && empty($alias_name) && empty($personal_photo)) {
            $this->ajaxJson(false, '参数有误~');
        }
        if (!$password) {
            $this->ajaxJson(false, '密码不能为空~');
        }
        if ($mobile && !valid_mobile($mobile)) {
            $this->ajaxJson(false, '手机号码有误，请检查~');
            if ($this->user->findByCount(['mobile_phone' => $mobile])) {
                $this->ajaxJson(false, '手机号不能重复~');
            }
        }
        if ($sex_id && !in_array($sex_id, [0, 1, 2])) {
            $this->ajaxJson(false, '性别不合法~');
        }
        if ($birthday) {
            $isDate = strtotime($birthday) ? strtotime($birthday) : false;
            if (!$isDate) {
                $this->ajaxJson(false, '生日不合法~');
            }
        }
        $useData = [
            'user_name'      => $username,
            'mobile_phone'   => $mobile,
            'alias_name'     => $alias_name,
            'user_email'     => $user_email,
            'user_type'      => $user_type,
            'parent_id'      => $parent_id,
            'pw'             => $password,
            'personal_photo' => $personal_photo,
            'contacter'      => $contacter,
            'sex_id'         => $sex_id,
            'birthday'       => $birthday,
            'created_at'     => date('Y-m-d H:i:s')
        ];
        $this->db->trans_begin();
        $uid = $this->user->insert($useData);
        $userAccount = [
            'uid'         => $uid,
           
        ];
        $accountId = $this->user_account->insert($userAccount);
        if (!$uid && !$accountId || $this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $this->ajaxJson(false, '网络繁忙，请稍候再试');
        }
        $this->db->trans_commit();

        //如果注册成功，则获取数据
        $data = $this->loginInfo(['uid' => $uid]);
        if (!$data['status']) {
            $this->ajaxJson($data);
        }
        $this->ajaxJson(true, '', $data['msg']);
    }

    /**
     * 修改用户信息
     */
    public function modifyUserInfo()
    {
        $uid            = (int)$this->input->get_post('uid'); //必传
        $alias_name     = $this->input->get_post('alias_name') ? $this->input->get_post('alias_name') : '';
        $password       = $this->input->get_post('password') ? $this->input->get_post('password') : '';
        $personal_photo = $this->input->get_post('personal_photo') ? $this->input->get_post('personal_photo') : '';
        $contacter      = $this->input->get_post('contacter') ? $this->input->get_post('contacter') : ''; //真实姓名
        $sex_id         = $this->input->get_post('sex_id') ? $this->input->get_post('sex_id') : ''; //0-保密，1-男 2-女
        $birthday       = $this->input->get_post('birthday') ? $this->input->get_post('birthday') : '0000-00-00'; //生日
        if (!$uid) {
            $this->ajaxJson(false, '参数有误~');
        }
        if (empty($alias_name) && empty($personal_photo) && empty($contacter) && empty($sex_id) && empty($birthday)) {
            $this->ajaxJson(false, '修改信息不能为空~');
        }
        if ($sex_id && !in_array($sex_id, [0, 1, 2])) {
            $this->ajaxJson(false, '性别不合法~');
        }
        if ($birthday) {
            $isDate = strtotime($birthday) ? strtotime($birthday) : false;
            if (!$isDate) {
                $this->ajaxJson(false, '生日不合法~');
            }
        }
        $params = [];
        if ($alias_name) {
            $params['alias_name'] = $alias_name;
        }
        if ($password) {
            $params['pw'] = $password;
        }
        if ($personal_photo) {
            $params['personal_photo'] = $personal_photo;
        }
        if ($contacter) {
            $params['contacter'] = $contacter;
        }
        if ($sex_id) {
            $params['sex_id'] = $sex_id;
        }
        if ($birthday && $birthday != '0000-00:00') {
            $params['birthday'] = $birthday;
        }
        if (empty($params)) {
            $this->ajaxJson(false, '修改不能为空~');
        }
        $update = $this->user->update($params, ['uid' => $uid]);
        if ($update) {
            $this->ajaxJson(true, '修改成功~');
        } else {
            $this->ajaxJson(false, '修改失败~');
        }
    }

    //如果注册成功，则获取数据
    private function loginInfo($params = [])
    {
        $result = $this->user->findOne($params);
        if ($result->num_rows() <= 0) {
            return ['status' => true, 'msg' => '账号信息有误或不存在~'];
        }
        $res = $result ->row(0);
        $userType = $this->usertype($res->user_type);
        $data = array(
            'ACT_UID'      => $res->uid,
            'ACT_UTID'     => $res->user_type,
            'ACT_TYPENAME' => urlencode($userType['type_zh']),
            'ACT_TYPE'     => $userType['type_en'],
            'ACT_EXTRA'    => $res->extra,
            'ALIAS_NAME'   => urlencode($res->alias_name),
            'OWNER_ID'     => $res->uid,
            'OWNER_NAME'   => $res->user_name,
            'PARENT_ID'    => $res->parent_id,
            'PERSONAL_PHOTO'  => urlencode($res->personal_photo),
        );
        return ['status' => true, 'msg' => $data];
    }
}